package com.lynx.system.controller;

import com.lynx.system.beans.GlobalVO;
import com.lynx.system.beans.User;
import com.lynx.system.feign.UserFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserFeign userFeign;

    @GetMapping("redirect/{location}")
    public String redirect(@PathVariable("location") String location){
        return location;
    }

    @GetMapping("/findAll")
    @ResponseBody
    public Object findAll(int page,int limit){
        Object users = userFeign.findAll(page, limit);
        int count = (int)userFeign.findCount();
        GlobalVO result = new GlobalVO(0,"",count,users);
        return result;
    }
    @GetMapping("/findCount")
    public Object findCount(){
        return userFeign.findCount();
    }
    @GetMapping("/findById/{id}")
    public Object findById(@PathVariable long id){
        return  userFeign.findById(id);
    }
    @PostMapping("/addUser")
    public Object addUser(User user){
        userFeign.addUser(user);
        return "redirect:redirect/user_manage";
    }
    @PostMapping("/editUser")
    public Object editUser(@RequestBody User user){
        return userFeign.editUser(user);
    }
    @GetMapping("/removeUser/{id}")
    public Object removeUser(@PathVariable int id){
        userFeign.removeUser(id);
        return "redirect:/user/redirect/user_manage";
    }
}
